🌟 Preview: Browser Functions - Deploy your web automation code directly on Browserbase with browser functions. Scale your act() automations in the cloud with zero infrastructure setup. Reach out to hello@browserbase.com to get beta access.

Deploy on Vercel

Securely run Stagehand on Browserbase inside a Vercel Function. This guide shows a minimal, production-safe HTTP endpoint you can call directly or on a schedule.

1. Install Vercel CLI

To download and install Vercel CLI, run one of the following commands:
pnpm i -g vercel

2. Project layout

your-project/
  api/
    run.ts
  package.json
  tsconfig.json
  vercel.json
Create the structure with:
mkdir -p api
touch api/run.ts package.json vercel.json tsconfig.json

3. api/run.ts (Node.js runtime)

// api/run.ts
import type { VercelRequest, VercelResponse } from "@vercel/node";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";

export default async function handler(req: VercelRequest, res: VercelResponse): Promise<void> {
  try {
    const stagehand = new Stagehand({
      env: "BROWSERBASE",
      apiKey: process.env.BROWSERBASE_API_KEY!,
      projectId: process.env.BROWSERBASE_PROJECT_ID!,
      modelName: "google/gemini-2.5-flash",
      modelClientOptions: {
        apiKey: process.env.GOOGLE_API_KEY!,
      },
      // optional session params
      browserbaseSessionCreateParams: {
        projectId: process.env.BROWSERBASE_PROJECT_ID!,
        region: "us-west-2",
        browserSettings: {
          blockAds: true,
        },
      },
    });

    await stagehand.init();
    const page = stagehand.page;

    await page.goto("https://www.stagehand.dev/");
    await page.act("click the evals button");

    const { extraction } = await page.extract("extract the fastest model");
    const data = { model: extraction ?? "" };

    await stagehand.close();

    res.status(200).json({ ok: true, data: data.model });
  } catch (err: unknown) {
    const msg = err instanceof Error ? err.message : String(err);
    res.status(500).json({ ok: false, error: msg });
  }
}

4. package.json

{
    "name": "bb-stagehand-on-vercel",
    "private": true,
    "type": "module",
    "engines": { "node": ">=18" },
    "dependencies": {
      "@browserbasehq/stagehand": "^2.4.3",
      "zod": "^3.25.0"
    },
    "devDependencies": {
      "typescript": "^5.6.0",
      "@types/node": "^20.12.12",
      "@vercel/node": "^3.2.20"
    }
}

5. tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "node",
    "outDir": ".vercel/output/functions",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "types": ["node"]
  },
  "include": ["api/**/*.ts"]
}

6. vercel.json

{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "functions": {
    "api/run.ts": {
      "maxDuration": 60
    }
  }
}
See Vercel’s configuring functions docs for more details. Link your local folder to a Vercel project before configuring environment variables:
# authenticate if needed
vercel login

# link the current directory to a Vercel project (interactive)
vercel link

8. Environment variables

Do not commit .env in production. Add variables via Vercel CLI:
vercel env add BROWSERBASE_API_KEY
vercel env add BROWSERBASE_PROJECT_ID
# (and your model key if needed)
vercel env add GOOGLE_API_KEY
See also: Browser Environment for details on required variables.

9. Test locally

Replicate the Vercel environment locally to exercise your Function before deploying. Run from the project root.
# ensure dependencies are installed
npm install

# start the local Vercel dev server
vercel dev --listen 5005

10. Deploy

vercel
vercel --prod

Execute the function

curl -X POST https://<your-deployment>/api/run

Optional: Cron on Vercel

Hit the same endpoint on a schedule by extending vercel.json:
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "functions": {
    "api/run.ts": {
      "maxDuration": 60
    }
  }
  },
  "crons": [
    { "path": "/api/run", "schedule": "0 * * * *" }
  ]
}

Features

  • No local browsers needed with env: "BROWSERBASE". Browserbase provides the browsers.
  • Fast functionality: Offload browser work to Browserbase and return JSON promptly.
  • Long-running tasks: Raise maxDuration and/or consider Edge runtime limits depending on plan.